package valve_test.DAO.Impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

import org.hibernate.Query;
import org.hibernate.Session;
import valve_test.logic.valves;
import valve_test.util.HibernateUtil;
import valve_test.DAO.IerarchyDAO;
import valve_test.logic.ierarchy;
/**
 * Created with IntelliJ IDEA.
 * User: Shinigami
 * Date: 11.03.14
 * Time: 22:11
 * To change this template use File | Settings | File Templates.
 */
public class IerarchyDAOImpl implements IerarchyDAO{
    public void addIerarchy(ierarchy ierarch) throws SQLException {
        Session session = null;
        try {

            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(ierarch);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }



    public void updateIerarchy(ierarchy ierarch) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(ierarch);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    public ierarchy getIerarchyById(Long id) throws SQLException {
        Session session = null;
        ierarchy ierarch = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            ierarch = (ierarchy) session.load(ierarchy.class, id);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return ierarch;
    }

    public List<ierarchy> getAllIerarchies() throws SQLException {
        Session session = null;
        List<ierarchy> ierarchs = new ArrayList<ierarchy>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            ierarchs = session.createCriteria(ierarchy.class).list();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return ierarchs;
    }

    public List<ierarchy> getIerarchiesByParent(Long p_id) throws SQLException {
        Session session = null;
        List<ierarchy> ierarchs = new ArrayList<ierarchy>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query query = session.createQuery(
                    " select ierarchy from ierarchy in class ierarchy where ierarchy.p_id = :p_id").setLong("p_id", p_id);
            ierarchs = query.list();
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return ierarchs;
    }

    public List<ierarchy> getIerarchiesByChild(Long c_id) throws SQLException {
        Session session = null;
        List<ierarchy> ierarchs = new ArrayList<ierarchy>();
        try {
            if (c_id != null){
                session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                Query query = session.createQuery(
                        " select ierarchy from ierarchy in class ierarchy where ierarchy.c_id = :c_id").setLong("c_id", c_id);
                ierarchs = query.list();
                session.getTransaction().commit();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return ierarchs;
    }

    public void deleteIerarchy(ierarchy ierarch) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(ierarch);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
